package com.own.demo.geodemo;

import com.alibaba.fastjson.JSONObject;
import com.own.demo.geogrid.BoundingBoxUtil;
import com.own.demo.geogrid.UtilsGeoGrid;
import com.own.demo.utils.FileToStringUtils;
import com.own.demo.utils.RegionUtil;
import com.own.demo.utils.geo.PoiTypeEnums;
import com.own.demo.utils.geo.buffer.LineBufferImpl;
import com.own.demo.utils.geo.buffer.PolygonBufferImpl;
import com.own.demo.vo.PointVo;
import org.junit.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.opengis.geometry.BoundingBox;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class GeoGridPoint {

    static PolygonBufferImpl polygonBuffer = new PolygonBufferImpl();
    static LineBufferImpl lineBuffer = new LineBufferImpl();

    @Test
    public void testGeoArea() {
        System.out.println("开始执行");
        File geoZJ = new File("E:\\ownCode\\emg-geojson\\src\\test\\java\\com\\own\\demo\\geodemo\\device.txt");
        String geoData = FileToStringUtils.txt2String(geoZJ).split("\\|")[0];
        String[] adasStr = geoData.split("\\,");
        int row = adasStr.length;
        System.out.println("行政区域围栏点数量：" + row);

        List<Double> lonList = new ArrayList(100000);
        List<Double> latList = new ArrayList(100000);

        for (int i = 0; i < row; i++) {
            String[] points = adasStr[i].split(" ");
            int pointLength = points.length;
            if (pointLength > 2) {
                System.out.println("错误采集点");
                continue;
            }

            lonList.add(Double.valueOf(points[0]));
            latList.add(Double.valueOf(points[1]));
        }

        System.out.println("lon长度" + lonList.size());
        System.out.println("lat长度" + latList.size());

        double minLon = Collections.min(lonList);
        double maxLon = Collections.max(lonList);
        double minLat = Collections.min(latList);
        double maxLat = Collections.max(latList);
        try {
            BoundingBox boundingBox = BoundingBoxUtil.getBoundingBox(minLon, maxLon, minLat, maxLat);
            Geometry geometry = PoiTypeEnums.geometry(geoData, PoiTypeEnums.POLYGON.getCode());
            double sideLen = 0.09;
            List<Geometry> geometries = new UtilsGeoGrid().gridGeometryInfo(geometry, boundingBox, true, sideLen);
            // Map<String, List<Double>> dataFenceMap = ToPointLine.goGeoToLonLatMap(geometries, geometry);

            Map<String, Coordinate> mapPint = ToPointLine.goGPSMap(geometries);

            List<Geometry> geometriesAll = new UtilsGeoGrid().gridGeometryInfo(geometry, boundingBox, false, sideLen);
            Map<String, List<Double>> dataAllFenceMap = ToPointLine.goGeoToLonLatMap(geometriesAll, geometry);

            List<Double> allLon = dataAllFenceMap.get("lon");
            List<Double> allLat = dataAllFenceMap.get("lat");

            int allX = allLon.size();
            int ally = allLat.size();
            PointVo[][] pointVos = new PointVo[allX][ally];

            for (int i = 0; i < allX; i++) {
                double x = allLon.get(i);
                for (int h = 0; h < ally; h++) {
                    double y = allLat.get(h);
                    String pointStr = x + "," + y;

                    PointVo pointVo= new PointVo(x,y,i,h,0);

//                    Coordinate coordinate=mapPint.get(pointStr);
//                    //不可达
//                    if(coordinate == null){
//                        //边界内不可达
//                        pointVo.setIsObstacle(2);
//                    }else{
                       boolean flag= isArrive(geometries,x,y);
                       if(flag){
                           pointVo.setIsObstacle(1);
//                       }
                    }
                    pointVos[i][h]=pointVo;
                    System.out.println(JSONObject.toJSONString(pointVo));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isArrive(List<Geometry> geometries,double x,double y) {
        int row = geometries.size();
        for (int i = 0; i < row; i++) {
            Geometry geometry = geometries.get(i);
            String data = polygonBuffer.gpsToString(geometry.getCoordinates());
            String center = x + "," + y;
            return RegionUtil.polygonCheck(center, data);
        }
        return false;
    }
}
